package org.leetcode.easy.leetcode14;

public class Solution
{
    public String longestCommonPrefix(String[] strs)
    {

        if (strs.length==0)
        {
            return "";
        }

        if (strs.length==1)
        {
            return strs[0];
        }

        String commonPrefix = getLongest(strs[0], strs[1]);

        for (int i = 2; i < strs.length; i++) {
            if (commonPrefix=="")
            {
                return commonPrefix;
            }else {
                commonPrefix=getLongest(commonPrefix,strs[i]);
            }
        }
        return commonPrefix;
    }


    public  String getLongest(String str1,String str2)
    {

        String temp;

        if (str1.length()>str2.length())
        {
            temp=str1;
            str1=str2;
            str2=temp;
        }

        String[] split1 = str1.split("");
        String[] split2 = str2.split("");

        for (int i = 0; i < split1.length; i++) {

            for (int j = 0; j < split1.length; j++) {
                if (!split1[i].equals(split2[j]))
                {
                   return str1.substring(0,i);
                }
                else {
                    i+=1;
                }
            }
        }
        return str1;
    }


    public static void main(String[] args) {
//        int longest = getLongest("abcdehjk", "abcd");
//        String str = getLongest("abcdef", "abcde");
//        System.out.println(str);
    }
}
